package com.asiainfo.service;
/*
 * 
 * 课程预订service
 */
import java.util.Date;

import com.asiainfo.bean.Coach;
import com.asiainfo.bean.Course;
import com.asiainfo.bean.Schedule;
import com.asiainfo.bean.Subscribe;
import com.asiainfo.bean.VIP;
import com.asiainfo.dao.Subscribe_Dao;

public class SubscribeService {
	@SuppressWarnings("deprecation")
	//预订课程
	public void addSubscribe(String[] args) {
		if (args.length != 3) {
			System.out.println("对不起，请输入合法参数");
			return;
		}
		Date date = new Date(Integer.parseInt(args[1].substring(0, 3)), Integer.parseInt(args[1].substring(4, 5)),
				Integer.parseInt(args[1].substring(6, 7)), Integer.parseInt(args[1].substring(9, 10)),
				Integer.parseInt(args[1].substring(11, 12)));
		ScheduleService ss = new ScheduleService();
		Schedule schedule = ss.findScheduleByTime((int) date.getTime());
		if (schedule == null) {
			System.out.println("对不起，不存在指定时间的课程");
			return;
		}
		VipService vipservice = new VipService();
		VIP vip = vipservice.findVipByName(args[2]);
		if (vip == null) {
			System.out.println("对不起，不存在指定的会员");
			return;
		}

		Subscribe subscribe = findSubscribeByVipandSchedule(vip.getId(), schedule.getSchedule_id());
		if (subscribe != null) {
			System.out.println("您已预订过该课程");
			return;
		}
		subscribe = new Subscribe(schedule.getSchedule_id(), vip.getId(), 0, (int) new Date().getTime());
		Subscribe_Dao sd = new Subscribe_Dao();
		int result = sd.addsubscribe(subscribe);
		if (result > 0) {
			CourseService cs = new CourseService();
			Course course = cs.findCourseById(schedule.getSchedule_course());
			CoachService coachService = new CoachService();
			Coach coach = coachService.findCoachById(schedule.getSchedule_coach());
			System.out.println(vip.getName() + "预订成功,请于" + Integer.parseInt(args[1].substring(0, 3)) + "年"
					+ Integer.parseInt(args[1].substring(4, 5)) + "月"
					+ Integer.parseInt(args[1].substring(6, 7)) + "日"
					+Integer.parseInt(args[1].substring(9, 10))+":"+Integer.parseInt(args[1].substring(11, 12))
					+"准时到剑瑜伽馆，修炼"+coach.getCoach_name()+"教练的"+course.getCourse_name()+".");
		} else {
			System.out.println("课程预订失败");
		}
	}
	//取消预订
	@SuppressWarnings("deprecation")
	public void cancelSubscribe(String[] args) {
		if (args.length != 3) {
			System.out.println("对不起，请输入合法参数");
			return;
		}
		Date date = new Date(Integer.parseInt(args[1].substring(0, 3)), Integer.parseInt(args[1].substring(4, 5)),
				Integer.parseInt(args[1].substring(6, 7)), Integer.parseInt(args[1].substring(9, 10)),
				Integer.parseInt(args[1].substring(11, 12)));
		if (date.getTime() - new Date().getTime() < 60 * 2 * 60) {
			System.out.println("取消预订课程，必须在开课两小时之前");
			return;
		}
		ScheduleService ss = new ScheduleService();
		Schedule schedule = ss.findScheduleByTime((int) date.getTime());
		if (schedule == null) {

			return;
		}
		VipService vipservice = new VipService();
		VIP vip = vipservice.findVipByName(args[2]);
		if (vip == null) {

			return;
		}

		Subscribe subscribe = findSubscribeByVipandSchedule(vip.getId(), schedule.getSchedule_id());
		if (subscribe == null) {
			System.out.println("您未预订该课程");
			return;
		}
		subscribe.setSubscribe_isdelete(1);
		Subscribe_Dao sd = new Subscribe_Dao();
		int result = sd.updatesubscribe(subscribe);
		if (result > 0) {
			System.out.println("课程取消成功");
		} else {
			System.out.println("课程取消失败");
		}
	}
	//根据会员和排课查找预订信息
	public Subscribe findSubscribeByVipandSchedule(int vip_id, int course_id) {
		Subscribe_Dao sd = new Subscribe_Dao();
		return sd.findsubscribeByVipandSchedule(vip_id, course_id);
	}
}
